package com.hw.dagl.controller;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.hw.dagl.model.Frm_Department;
import com.hw.dagl.model.Sys_User;
import com.hw.dagl.model.Vehicle;
import com.hw.dagl.service.UserService;
import com.hw.dao.utils.ControllerUtil;
import com.hw.dao.utils.Page;
import com.hw.interceptor.SystemLog;
import com.hw.utils.EncryptUtil;

@Controller
public class UserController extends BaseController{
	
	@Autowired
	private UserService userServiceImpl;
	
	/*
	 * 跳转到用户管理页面
	 */
	@RequestMapping(method=RequestMethod.GET)
	public String userHome(HttpServletResponse response){
		return "user/userHome";
	}
	/*
	 * 分页显示用户信息
	 */
	@RequestMapping(method=RequestMethod.POST)
	public void findUserForPage(HttpServletRequest request,HttpServletResponse response,Page<Sys_User> pages,Sys_User info){
		Sys_User user = (Sys_User) request.getSession().getAttribute("user");//得到当前登录角色
		String userSsbm = user.getGlbm();
		Page<Sys_User> list = userServiceImpl.findUser(pages, info,userSsbm);
		ControllerUtil.renderJson(response, list, "encoding:UTF-8","no-cache:false");
	}
	/**
	 * 根据用户编号查询用户信息
	 * @param response
	 * @param info
	 */
	@RequestMapping(method=RequestMethod.POST)
	public void findUserByYhdh(HttpServletResponse response,Sys_User info){
		Sys_User list = userServiceImpl.findUserByYhdh(info);
		if(list!=null){
			list.setMm("111111");
		}
		ControllerUtil.renderJson(response, list, "encoding:UTF-8","no-cache:false");
	}
	@RequestMapping(method=RequestMethod.GET)
	public void findUserByYhdh2(HttpServletResponse response,Sys_User info){
		Sys_User list = userServiceImpl.findUserByYhdh(info);
		if(list!=null){
			list.setMm("111111");
		}
		ControllerUtil.renderJson(response, list, "encoding:UTF-8","no-cache:false");
	}
	
	/**
	 * 批量删除用户
	 * @param response
	 * @param ids 用户帐号
	 */
	@RequestMapping(method=RequestMethod.POST)
	public void delUser(HttpServletResponse response,String ids){
		boolean flag = userServiceImpl.delUser(ids);
		ControllerUtil.renderJson(response, flag, "encoding:UTF-8","no-cache:false");
	}
	/**
	 *	保存用户信息
	 * @param request
	 * @param response
	 * @param info
	 */
	@RequestMapping(method=RequestMethod.POST)	
	public void saveUser(HttpServletRequest request,HttpServletResponse response,Sys_User info,String saveLx,String zhyxqStr,String mmyxqStr){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if(!zhyxqStr.equals("")){//修改
			try {
				info.setZhyxq(sdf.parse(zhyxqStr));
			} catch (ParseException e) {
				// TODO 自动生成�?catch �?
				e.printStackTrace();
			}
		}
		if(!mmyxqStr.equals("")){//修改
			try {
				info.setMmyxq(sdf.parse(mmyxqStr));
			} catch (ParseException e) {
				// TODO 自动生成�?catch �?
				e.printStackTrace();
			}
		}
		if(info.getUpload_zp().getSize()>0){
			CommonsMultipartFile file = info.getUpload_zp();
			String desDic = request.getSession().getServletContext().getRealPath("/")+"upload"+File.separatorChar+"userpic"+File.separatorChar;
			String savePath = super.updateFile(request, file, desDic);
			if(StringUtils.isNotEmpty(savePath)){
				info.setZp(savePath);
			}
		}
		if(!saveLx.equals("")){//修改
				boolean flag=userServiceImpl.updateUser(info);
				ControllerUtil.renderJson(response, flag, "encoding:UTF-8", "no-cache:false");
		}else{//新增
			Sys_User user= (Sys_User)request.getSession().getAttribute("user");
			info.setCjr(user.getXm());
			boolean flag=userServiceImpl.saveUser(info);
			ControllerUtil.renderJson(response, flag, "encoding:UTF-8", "no-cache:false");
		}
		
	}
    /**
     * 查询�?��部门
     * @param response
     */
	@RequestMapping(method=RequestMethod.POST)
	public void findBm(HttpServletResponse response){
		List<Frm_Department> list = userServiceImpl.findBm();
		ControllerUtil.renderJson(response, list, "encoding:UTF-8","no-cache:false");
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public void udpatePasswd(HttpServletRequest request,HttpServletResponse response,Sys_User info){
		Sys_User user= (Sys_User)request.getSession().getAttribute("user");
		info.setYhdh(user.getYhdh());
		info.setMm(EncryptUtil.encrypt(info.getMm().trim(), "MD5"));
		boolean flag= userServiceImpl.updatePasswd(info);
		ControllerUtil.renderJson(response, flag, "encoding:UTF-8","no-cache:false");
	}
	@RequestMapping(method=RequestMethod.POST)
	public void selectSsbmOfUser(HttpServletRequest request,HttpServletResponse response){
		Sys_User user= (Sys_User)request.getSession().getAttribute("user");
		Sys_User userInfo= userServiceImpl.selectSsbmOfUser(user);
		ControllerUtil.renderJson(response, userInfo, "encoding:UTF-8","no-cache:false");
	}
}
